<?php
@session_start();
@ob_start();
error_reporting (E_ALL ^ E_NOTICE);

require("../../includes/config.inc.php"); 
require("../../includes/Database.class.php"); 


require_once("../../includes/GPSFunction.php");

$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 
$db->connect(); 
$temp="0";
//print_r($_GET);
function chk_folder($filename)
{
	$fp_load = @fopen("$filename", "rb");
	if ( $fp_load )
	{
		return true;
	}
	else
	{
		return false;
	}
}
function liveKmsAllPerDay($lData)
{ 

	$timeArr = array();
	$ptArr = array();
	$tmpDist = -1;
	
	$cnt = 0;
	$totalDistance = 0;
	$data1=explode("#",$lData);
	for($j1=0;$j1<count($data1);$j1++)
	{
		$data2=explode("@",$data1[$j1]);

		if(count($data2)>1)
		{
			$data3=explode(",",$data2[1]);
			$geodate = $data3[8];
			$geoTime = $data3[9];
			$curTime = explode(":",$data3[9]);
			$curTime = (($curTime[0] * 60) + $curTime[1]);
			//echo "<br>";
			$mph = round($data3[3]);
			$pos1=calLat($data3[2]);
			$pos2=calLong($data3[1]);
			
			if($pos1 > 0 && $pos2 > 0)
			{
				if(!in_array($geoTime,$timeArr))
				{
					$tmpLatLng = $pos1.",".$pos2;
					$ptArr[] = $tmpLatLng;

					if($cnt==0)
					{									
						$pits1 =  $pos1;
						$pits2 =  $pos2;
					}
					else
					{
						$pits3 =  $pos1;
						$pits4 =  $pos2;
						$dist = getDistance($pits1, $pits2, $pits3, $pits4);
						$totalDistance += $dist;
						$pits1 =  $pits3;
						$pits2 =  $pits4;
					}
					$cnt++;
				}
				
			}
		}
		array_push($timeArr,$geoTime);
		//exit;
	}
	$ptArr = implode("@",$ptArr); 
	$finalData = round($totalDistance)."#".$ptArr;
	return $finalData;
}

function gpspathFun($clientId,$date_offline,$deviceIMEI,$sTime,$eTime)
{
	//$date_offline = $_GET[date_offline];
	
	$path1=DATAPATH.date('d-m-Y',strtotime($date_offline))."/".$deviceIMEI.".txt";
	
	if(chk_folder($path1))
	{
		$file1 = @fopen($path1, "r");
		if($file1)
		{
			$i=0;
			while(!feof($file1))
			{
			  $data1= fgets($file1);				 
			}
			$data = getSortedData($data1);
			return $data;
			fclose($file1);
		}
		else
		{
			$data=0;
			//storeData($data);
		}
	}
		
} //$path1=$dataPath."client_".$_SESSION[clientID]."/".date('d-m-Y',strtotime($_GET["date_offline"]))."/".$_GET["deviceIMEI"].".txt";
if(isset($_GET[date_offline]) && isset($_GET[deviceIMEI]))
{
	$path1=DATAPATH.date('d-m-Y',strtotime($_GET["date_offline"]))."/".$_GET["deviceIMEI"].".txt";	
	$getLiveData = "SELECT * FROM tb_livegps_info WHERE tli_imei = '".$_GET[deviceIMEI]."'";
	
	$exeLiveData = $db->query($getLiveData);
	if($db->affected_rows > 0)
	{
		$fetLiveData = $db->fetch_array($exeLiveData);
		$odoMeter = $fetLiveData[tli_distance];
		$devData = explode("^",getDeviceName($_GET["deviceIMEI"]));  
		$devName = $devData[0];
		$devModel = $devData[1];
		$devIcon = $devData[5];
		$inPort = $devData[2];
		$outPort = $devData[3];
		$analogPort = $devData[4];
		storeData($fetLiveData[tli_parameter],$devName,$devModel,($fetLiveData[tli_today]),$inPort,$outPort,$analogPort,$devIcon);
	}
}
function getDeviceName($imeiNo)
{
		$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 
		$db->connect(); 
		$getDevName = "SELECT * FROM tb_deviceinfo,tb_device_model WHERE id = di_deviceModel AND di_imeiId =".$imeiNo." AND di_status = 1";
		
		
		$resultRow = $db->query($getDevName);
		$deviceRecord = $db->fetch_array($resultRow);
		if($deviceRecord[di_deviceName])
			return $deviceRecord[di_deviceName]."^".ucfirst($deviceRecord[tdm_aliasName])."^".$deviceRecord[di_inputPort]."^".$deviceRecord[di_outputPort]."^".$deviceRecord[di_analogPort]."^".$deviceRecord[di_mapIcon];
		else
			return $deviceRecord[di_deviceId]."^".ucfirst($deviceRecord[tdm_aliasName])."^".$deviceRecord[di_inputPort]."^".$deviceRecord[di_outputPort]."^".$deviceRecord[di_analogPort]."^".$deviceRecord[di_mapIcon];
}


function storeData($data,$devName,$devModel,$km,$inPort,$outPort,$analogPort,$devIcon)
{
	$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 
	$db->connect(); 
	
	$xml = '<gps>';
	$timeArr= array();
	$ptArr = array();
	$totalDistance=0;
	
	
	$ct = 0;
	$j1=0;
	$data1=explode("#",$data);
	$data2=explode("@",$data1[count($data1)-2]);
	if(count($data2)>1)
	{
		$data3=explode(",",$data2[1]);
		//print_r($data3);
		$geoTime = date("h:i:s A",strtotime($data3[9]));
		$geodate = $data3[8];

		$pos1=calLat($data3[2]);
		$pos2=calLong($data3[1]);
		if($pos1>0 && $pos2>0)
		{			
			if(!in_array($geoTime,$timeArr))
			{
				$tmpLatLng = $pos1.",".$pos2;
				if(!in_array($tmpLatLng,$ptArr))
				{					
					$mph = round($data3[3]);
					$direction = $data3[4];
					$altitute = $data3[5];
					$deviceIMEI = $data3[0];
					$sessionID = $_GET["sessionID"] ;
					$accuracy = $data3[6];
					$extraInfo = $data3[6];
					
					//echo $analogPort;		//$inPort.','.$outPort.','.
					/*if($inPort!='')
					{
						$inPort = explode("#",$inPort);
						for($k=0;$k<count($inPort)-1;$k++)
						{						
							$inp = explode("_",$inPort[$k]);
							$digInputLabel .= $inp[0]."#";
							$digInputValue .= splitParameter($inp[1],$data3[7])."#";	
						}
					}
					
					if($outPort!='')
					{
						$outPort = explode("#",$outPort);
						for($k=0;$k<count($outPort)-1;$k++)
						{						
							$out = explode("_",$outPort[$k]);
							$digOutputLabel .= $out[0]."#";
							$digOutputValue .= splitParameter($out[1],$data3[7])."#";	
						}
					}
					
					if($analogPort!='')
					{						
						$analogPort = explode("@",$analogPort);
						for($k=0;$k<count($analogPort)-1;$k++)
						{						
							$d1 = explode("=",$analogPort[$k]);
							$analogLabel .= $d1[0]."#";
							$d2 = substr($d1[1],0,strpos($d1[1],"("));
							$d3 = str_replace(")","",substr($d1[1],(strpos($d1[1],"(")+1))); 							
							$d3 = explode("#",$d3);
							for($l = 0; $l < count($d3)-1; $l++)
							{
								$d4 = explode("_",$d3[$l]);
								$fuel_params[$d4[0]] = $d4[1];
							}
							$analogValue = getClosestAnalogValue($fuel_params,splitParameter($d2,$data3[7]))."#".splitParameter($d2,$data3[7]);
							$fuel_params = "";
						}		
					}*/
					$xml .='<locations latitude="'.$pos1.'" longitude="'.$pos2. '" speed="'.$mph. '" direction="'.$direction.'" altitute="'.$altitute.'" curTime = "'.$curTime.'" distance="'.floor($km).'" gpsTime="'.date("h:i:s A",strtotime($data3[9])).'" geodate="'.$geodate.'" deviceIMEI="'.$deviceIMEI. '" deviceName="'.$devName.'" sessionID="'.$sessionID.'"  extraInfo="'.$extraInfo.'"  route="'.$rtName.'"  deviceModel="'.str_replace("&","",$devModel).'" Parameter="'.$data3[7].'" icon = "'.$devIcon.'" />';
					
				}
			}
		}
	}
	
	array_push($timeArr,$geoTime);
	
	$xml .= '<OtherData totPt="'.$totPath.'" geoData="" geoPointName="" totalDist="'.floor($km).'" inPort = "'.$inPort.'" outPort = "'.$outPort.'" analogPort = "'.$analogPort.'"  />';	
	$xml .= '</gps>';
	
	header('Content-Type: text/xml');
	echo $xml;
}

?>
